﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Credera.Theta.API;

namespace Credera.Theta.Modules.Environments.Commands
{
    [Command("CreateEnvironment")]
    [Synopsis("Creates a new environment")]
    class CreateEnvironmentCommand : InputCommand
    {
        [Parameter(ordinalPosition:1)]
        [Synopsis("The unique name of the environment")]
        public string Name { get; set; }

        public override void Invoke(CommandInvocationContext context)
        {
            if (EnvironmentLogic.Instance.EnvironmentsCollection.Environments
                .Exists(e => e.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)))
            {
                base.Out.Warning.WriteLine(string.Format("The environment name '{0}' is already being used by another environment", this.Name));
            }
            else
            {
                var newEnv = new Environments.Environment()
                {
                    Name = this.Name
                };

                EnvironmentLogic.Instance.EnvironmentsCollection.Environments.Add(newEnv);
                EnvironmentLogic.Instance.Save();

                base.Out.Object.Write(TableRecords.CreateEnvironmentRecordList(newEnv));               
            }
        }
    }
}
